GetStDevOfGridFloat Function

private function GetStDevOfGridFloat(grid, maskReal, maskInteger) result(stDev)

compute unbiased standard deviation of grid_real optionally constrained to a mask

Arguments

Type IntentOptional Attributes Name
type(grid_real), intent(in) :: grid
type(grid_real), intent(in), optional :: maskReal
type(grid_integer), intent(in), optional :: maskInteger

Return Value real(kind=float)


Variables

Type Visibility Attributes Name Initial
real(kind=float), public :: countCells
integer(kind=long), public :: i
integer(kind=long), public :: j
real(kind=float), public :: mean

Source Code

FUNCTION GetStDevOfGridFloat &
!
(grid, maskReal, maskInteger) &
!
RESULT (stDev)

IMPLICIT NONE

!Arguments with intent(in):
TYPE (grid_real), INTENT(IN) :: grid
TYPE (grid_real), OPTIONAL,  INTENT(IN) :: maskReal
TYPE (grid_integer), OPTIONAL,  INTENT(IN) :: maskInteger


!Local declarations:
REAL (KIND = float) :: mean
REAL (KIND = float) :: stDev
REAL (KIND = float) :: countCells
INTEGER (KIND = long) :: i, j
!---------------------------end of declarations--------------------------------
stDev = 0.

!check that grid and mask have the same coordinate reference system
IF (PRESENT (maskReal)) THEN
    IF ( .NOT. CRSisEqual(maskReal,grid) ) THEN
        CALL Catch ('error', 'GridStatistics',  &
        'calculate standard deviation: ', argument = &
        'coordinate reference system of mask differs from input grid' )
    END IF
    
    !get mean
    mean = GetMean (grid, maskReal = maskReal)

    DO j = 1, maskReal % jdim
        DO i = 1, maskReal % idim
            IF (maskReal % mat(i,j) /= maskReal % nodata) THEN
                stDev = stDev + ( grid % mat (i,j) - mean) **2.
                countCells = countCells + 1.
            END IF
        END DO
    END DO
ELSE IF (PRESENT (maskInteger)) THEN
    IF ( .NOT. CRSisEqual(maskInteger,grid) ) THEN
        CALL Catch ('error', 'GridStatistics',  &
        'calculate standard deviation: ', argument = &
        'coordinate reference system of mask differs from input grid' )
    END IF
    
    !get mean
    mean = GetMean (grid, maskInteger = maskInteger)

    DO j = 1, maskInteger % jdim
        DO i = 1, maskInteger % idim
            IF (maskInteger % mat(i,j) /= maskInteger % nodata) THEN
                stDev = stDev + ( grid % mat (i,j) - mean) **2.
                countCells = countCells + 1.
            END IF
        END DO
    END DO

ELSE

    !get mean
    mean = GetMean (grid)
    
    DO j = 1, grid % jdim
        DO i = 1, grid % idim
            IF (grid % mat(i,j) /= grid % nodata) THEN
                stDev = stDev + ( grid % mat (i,j) - mean) **2.
                countCells = countCells + 1.
            END IF
        END DO
    END DO
END IF

stDev = (stDev / (countCells - 1.)) ** 0.5

RETURN

END FUNCTION GetStDevOfGridFloat